package com.gln.bloger.service.impl;

import java.util.List;

import com.gln.bloger.constant.Constant;
import com.gln.bloger.dao.TagDao;
import com.gln.bloger.exception.BlogerException;
import com.gln.bloger.log.Logging;
import com.gln.bloger.pojos.Tag;
import com.gln.bloger.pojos.Website;
import com.gln.bloger.service.TagService;

public class TagServiceImpl implements TagService {

	private TagDao tagDao;
	
	private List<Tag> cachedHotTags;
	
	public TagDao getTagDao() {
		return tagDao;
	}

	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	public List<Tag> getHotTags(Website website) throws BlogerException {
		if(this.cachedHotTags != null && this.cachedHotTags.size() > 0){
			Logging.info("load hot tags from app cache");
		}else{
			this.cachedHotTags = getTagDao().getHotTags(website, Constant.HOT_TAG_SIZE);
		}
		return this.cachedHotTags;
	}
	
	public List<Tag> getTags(Website website) throws BlogerException {
		List<Tag> tags = getTagDao().list(website);
		return tags;
	}
	public Tag getTag(String id) throws BlogerException {
		Tag tag = getTagDao().get(id);
		return tag;
	}
	
	public Tag getTagByName(Website website, String name) throws BlogerException {
		Tag tag = getTagDao().getTagByName(website, name);
		return tag;
	}

	public void saveOrUpdateTag(Tag tag) throws BlogerException{
		this.getTagDao().saveOrUpdate(tag);
	}

	public void clearCachedHotTags() {
		if(this.cachedHotTags != null){
		    this.cachedHotTags.clear();
		}
	}
}
